﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using SubSonic;
using System.Data;
using System.Data.SqlClient;


public partial class Controls_FileDownload_FileDownload : System.Web.UI.UserControl
{

    private string _TaskId;
    public string TaskId
    {
        get { return _TaskId; }
        set
        {
            _TaskId = value;
        }
    }
   
     protected void Page_Load( object sender, EventArgs e )
    {
        if ( !Page.IsPostBack )
        {
          
            DataListFileDownload.DataSource = TblFile.FetchByParameter(TblFile.Columns.TaskId, Comparison.Equals, TaskId,
                                                                        SubSonic.OrderBy.Asc( "Date" ) );
            DataListFileDownload.DataBind();
            
        }
    }
    protected void ClickDownload(object sender, EventArgs e)
    {
        LinkButton linkButton = sender as LinkButton;
        DataListItem dataListItem = linkButton.NamingContainer as DataListItem;
        Guid id = new Guid(linkButton.CommandArgument.ToString());
        TblFileCollection tblFileCollection  = new TblFileCollection();
        tblFileCollection.LoadAndCloseReader( TblFile.FetchByParameter( 
            TblFile.Columns.Id , Comparison.Equals , id) );
        switch ( System.IO.Path.GetExtension( tblFileCollection[0].OriginalFileName ) )
        {
            case ".jpg":
                Response.ContentType = "image.jpg";
                break;
            case ".pdf":
                Response.ContentType = "application/pdf";
                break;
            case ".doc":
            case ".docx":
                Response.ContentType = "application/msword";
                break;
            case ".dwg":
                Response.ContentType = "image/vnd.dwg";
                break;
            default:
                Response.ContentType = "application/octet-stream";
                break;
        }
        
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + tblFileCollection[0].OriginalFileName.Replace( " " , "_" ));
       
            Response.TransmitFile(Server.MapPath("~/Files/" + tblFileCollection[0].FileName));
      
        Response.End();
    }
    protected void DataListFileDownload_DataBinding(object sender, EventArgs e)
    {
       
    }
}